<template>
  <div class="seat-head">
    <el-form
      :model="formData"
      :rules="rules"
      ref="form"
      label-width="85px"
      label-position="left"
    >
      <el-row>
        <el-form-item label="订单号" prop="id">
          <el-input v-model="formData.id" placeholder="请输入订单号"></el-input>
        </el-form-item>
        <el-button class="submit" @click="onSubmit">去选座</el-button>
      </el-row>
    </el-form>
  </div>
  <div class="seat">
    <div id="seats">
      <select-seat v-model:selected="seats"></select-seat>
    </div>
  </div>
  <div class="info">
    <div class="head">需要选座的乘客</div>
    <div>
      <el-card v-for="(customer, index) in customers" :key="index">
        <div>{{ '乘客姓名：' + customer.name }}</div>
        <div>{{ '身份证号：' + customer.id }}</div>
        <div>{{ '座位号：' + customer.seat }}</div>
      </el-card>
    </div>
    <div class="btn">
      <!-- {{ seats }} -->
      <el-button @click="confirm">确认</el-button>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, watch } from 'vue';
import SelectSeat from '@/components/Service/SelectSeat.vue';
import { Order } from '@/utils/api/order';
import { ElMessage } from 'element-plus';

export default defineComponent({
  name: 'SeatSelection',
  components: { SelectSeat },
  setup() {
    //表单数据验证
    const formData = ref({
      id: '',
    });
    const rules = {
      id: [{ required: true, message: '请输入订单号' }],
    };
    const form = ref(null);
    const onSubmit = () => {
      (form.value as any).validate((valid: boolean) => {
        if (valid) {
          Order.getOrder({
            orderId: formData.value.id,
          }).then((res) => {
            if (res.data.status === '200') {
              customers.value = res.data.customers;
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
      // console.log((form.value as any).validate);
      // console.log('formData.value: ', formData.value);
    };

    const seats = ref([]);
    const customers = ref([
      {
        name: '小明',
        id: '100100100',
        seat: '',
      },
      {
        name: '小明',
        id: '100100100',
        seat: '',
      },
      {
        name: '小明',
        id: '100100100',
        seat: '',
      },
    ]);
    watch(
      () => seats.value,
      () => {
        let sl = seats.value.length;
        let cl = customers.value.length;
        for (let i = 0; i < cl; i++) {
          if (i >= sl) {
            customers.value[i].seat = '';
          } else {
            customers.value[i].seat = seats.value[i];
          }
        }
      }
    );
    const confirm = () => {
      //提交座位数据
      Order.selectSeat({
        orderId: formData.value.id,
        seats: seats.value,
      })
        .then((res) => {
          if (res.data.status === '200') {
            ElMessage('选座成功');
          } else {
            ElMessage('选座失败');
          }
        })
        .catch((err) => {});
    };
    return {
      formData,
      rules,
      form,
      onSubmit,
      //
      seats,
      customers,
      confirm,
    };
  },
});
</script>

<style lang="scss" scoped>
@import '@/assets/css/config.scss';
.seat-head {
  :deep(.el-form-item__label) {
    font-size: 16px;
    font-weight: 600;
    padding-right: 10px;
  }
  padding: 50px 0;
  margin-left: 20%;
  .submit {
    height: 20px;
    width: 100px;
    margin-left: 50px;
    background-color: $themeRed;
    color: white;
    font-weight: 600;
  }
}
.seat {
  // background-color: #333;
  min-width: 25vw;
  min-height: 105vh;
  float: left;
  overflow: hidden;
  #seats {
    max-width: 25vw;
    max-height: 105vh;
    // background-color: #fff;
  }
}
.head {
  margin-top: 5vh;
  background-color: $themeLight;
  color: white;
  height: 4vh;
  line-height: 4vh;
  // float: left;
  padding: 1vh 6vh;
}
.info {
  margin-top: 2vh;
  padding: 5vh;
  width: 20vw;
  float: right;
  // margin-left: 30vw;
  .btn {
    position: relative;
    float: right;
    .el-button {
      background-color: $themeRed;
      color: white;
      width: 8vw;
      font-weight: 600;
      // font-size: 2rem;
    }
  }
}
.el-card {
  height: 13vh;
  div {
    height: 3vh;
    line-height: 3vh;
  }
}
</style>
